var call = null;

var Ajax = new function() {
	
	this.conn = false;
	
	this.connect = function() {
		if (window.XMLHttpRequest) { // Mozilla, Safari,...
            this.conn = new XMLHttpRequest();
            if (this.conn.overrideMimeType) {
                this.conn.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) { // IE
            try {
                this.conn = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    this.conn = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
	};
	
	this.xml = function(method,url,mode,callback) {
		call = callback;
		this.connect();
        this.conn.onreadystatechange = this.callXML;
        this.conn.open(method,url,mode);
        if( window.ActiveXObject ) {
        	this.send();	
        } else {
        	this.send(null);
        }
        if( method == 'POST' ) {
        	this.conn.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
        }        
	};
	
	this.text = function(method,url,mode,callback) {
		call = callback;
		this.connect();
        this.conn.onreadystatechange = this.callText;
        this.conn.open(method,url,mode);
        if( window.ActiveXObject ) {
        	this.send();	
        } else {
        	this.send(null);
        }
        if( method == 'POST' ) {
        	this.conn.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
        }  
	};
	
	this.open = function(method,url,mode,callback) {
		this.text(method,url,mode,callback);
	};
	
	this.get = function(url,mode,callback) {
		this.text('GET',url,mode,callback);
	};
	
	this.post = function(url,mode,callback) {
		this.text('POST',url,mode,callback);
	};

	this.getXML = function(url,mode,callback) {
		this.xml('GET',url,mode,callback);
	};
	
	this.postXML = function(url,mode,callback) {
		this.xml('POST',url,mode,callback);
	};
	
	this.getAsync = function(url,callback) {
		this.text('GET',url,true,callback);
	};
	
	this.postAsync = function(url,callback) {
		this.text('POST',url,true,callback);
	};

	this.getXMLAsync = function(url,callback) {
		this.xml('GET',url,true,callback);
	};
	
	this.postXMLAsync = function(url,callback) {
		this.xml('POST',url,true,callback);
	};
	
	this.getSync = function(url,callback) {
		this.text('GET',url,false,callback);
	};
	
	this.postSync = function(url,callback) {
		this.text('POST',url,false,callback);
	};

	this.getXMLSync = function(url,callback) {
		this.xml('GET',url,false,callback);
	};
	
	this.postXMLSync = function(url,callback) {
		this.xml('POST',url,false,callback);
	};
	
	this.send = function(param) {
		this.conn.send(param);
	};
	
	this.callText = function() {
		if( Ajax.conn.readyState == 4 ) {
			if( Ajax.conn.status == 200 ) {
				var text = escape(Ajax.conn.responseText);
				if( call == null ) {
					return;
				} else {
					eval(call+"(unescape('"+text+"'))");
				}
			}
		}
	};
	this.callXML = function() {
		if( Ajax.conn.readyState == 4 ) {
			if( Ajax.conn.status == 200 ) {
				var text = Ajax.createXML(Ajax.conn.responseXML);
				if( call == null ) {
					return;
				} else {
					eval(call+"('"+text+"')");
				}
			}
		}
	};
	this.createXML = function(text) {
		try	{ // Internet Explorer
  			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  			xmlDoc.async="false";
  			xmlDoc.loadXML(text);
  			return xmlDoc;
  		} catch(e) {
  			try { // Firefox, Mozilla, Opera, etc.
    			parser=new DOMParser();
    			return parser.parseFromString(text,"text/xml");
	    	} catch(e) {
	    		alert(e.message);
	    		return;
	    	}
  		}
	};	
};